/**
 * 获取元素节点信息
 * @param {String} id 
 */
function getElementInfo(id) {
  return new Promise(resolve => {
    const query = wx.createSelectorQuery();
    query.select("#" + id).boundingClientRect(rect => {
      resolve(rect);
    }).exec();
  })
}

/**
 * 获取系统信息
 */
function getSystemInfo() {
  // 获取设备系统信息，单位px
  const sysInfo = wx.getSystemInfoSync();
  // 获取原胶囊信息
  const oriCap = wx.getMenuButtonBoundingClientRect();
  // 状态栏高度
  const staBarHeight = sysInfo.statusBarHeight;
  // 屏幕宽度
  const scrWidth = sysInfo.screenWidth;
  // 屏幕高度
  const scrHeight = sysInfo.screenHeight;
  // 窗口宽度
  const winWidth = sysInfo.windowWidth;
  // 窗口高度
  const winHeight = sysInfo.windowHeight;
  // 胶囊宽度
  const capWidth = oriCap.width;
  // 胶囊高度
  const capHeight = oriCap.height;
  // 胶囊上边距
  const capTop = oriCap.top;
  // 胶囊右边距
  const capRight = scrWidth - oriCap.right;
  // 胶囊下边距
  const capBottom = oriCap.bottom - capHeight - staBarHeight;
  // 胶囊左边距
  const capLeft = oriCap.left;
  // 导航栏高度
  const navHeight = oriCap.bottom + capBottom;
  // 标题栏高度
  const titleHight = navHeight - staBarHeight;
  return {
    scrWidth,
    scrHeight,
    winWidth,
    winHeight,
    navHeight,
    staBarHeight,
    titleHight,
    capWidth,
    capHeight,
    capTop,
    capRight,
    capBottom,
    capLeft
  }
}

/**
 * 设置系统信息
 * @param {Object} page 
 */
function setSysInfo(page) {
  const app = getApp();
  const globalData = app.globalData;
  page.setData({
    scrWidth: globalData.scrWidth,
    scrHeight: globalData.scrHeight,
    winWidth: globalData.winWidth,
    winHeight: globalData.winHeight,
    navHeight: globalData.navHeight,
    staBarHeight: globalData.staBarHeight,
    titleHight: globalData.titleHight,
    capWidth: globalData.capWidth,
    capHeight: globalData.capHeight,
    capTop: globalData.capTop,
    capRight: globalData.capRight,
    capBottom: globalData.capBottom,
    capLeft: globalData.capLeft
  });
}

module.exports = {
  getElementInfo,
  getSystemInfo,
  setSysInfo
}